package com.biscuit.page.basicservice.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.biscuit.common.base.BaseResponse;
import com.biscuit.common.base.BaseResponseVoid;
import com.biscuit.page.basicservice.api.model.request.search.SearchSuggestAReq;
import com.biscuit.page.basicservice.api.model.response.search.SearchSuggestAResp;
import com.biscuit.page.basicservice.conf.handler.UserContextHandler;
import com.biscuit.page.basicservice.design.search.SearchHandlerContext;
import com.biscuit.page.basicservice.design.search.factory.SearchHandlerFactory;
import com.biscuit.page.basicservice.conf.handler.RequestContextHandler;
import com.biscuit.page.basicservice.model.entities.mysql.SysSearchLog;
import com.biscuit.page.basicservice.model.enums.SearchEngineTypeEnum;
import com.biscuit.page.basicservice.persistence.dao.SearchLogDao;
import com.biscuit.page.basicservice.utils.BaseExceptionUtil;
import com.biscuit.page.basicservice.utils.BaseResponseUtil;
import com.biscuit.page.basicservice.utils.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mazihao
 * @createTime 2024/3/29 13:30
 */
@Slf4j
@Service
public class SearchService {

    @Resource
    private SearchLogDao searchLogDao;

    public BaseResponse<SearchSuggestAResp> suggest(SearchSuggestAReq req) {
        // 参数校验
        BaseExceptionUtil.throwException(StringUtils.isNotBlank(req.getKeyword()), "搜索关键字为空");

        // 设计模式上下文对象
        SearchHandlerContext context = new SearchHandlerContext();
        context.setKeyword(req.getKeyword());
        context.setSearchEngineTypeEnum(SearchEngineTypeEnum.getSearchEngineTypeEnumByType(req.getType()));

        // 通过工厂获取搜索实现类，并进行联想词搜索
        List<SearchSuggestAResp.SuggestAResp> suggestList = SearchHandlerFactory.createStrategy(context).suggest();

        // 记录本地搜索记录
        SysSearchLog sysSearchLog = new SysSearchLog();
        sysSearchLog.setKeyword(context.getKeyword());
        sysSearchLog.setEngineType(context.getSearchEngineTypeEnum().getType());
        sysSearchLog.setUserId(UserContextHandler.isLogin() ?
                UserContextHandler.getUserId() : 0);
        sysSearchLog.setIp(RequestContextHandler.getContext().getIp());
        sysSearchLog.setSuggestList(CollectionUtils.isNotEmpty(suggestList) ? JSON.toJSONString(suggestList) : "");
        this.searchLogDao.insertAndIncrement(sysSearchLog);

        // 返回关联词
        SearchSuggestAResp responseData = new SearchSuggestAResp();
        responseData.setLogId(sysSearchLog.getId());
        responseData.setList(suggestList);
        return BaseResponseUtil.success(responseData);
    }

    public BaseResponse<BaseResponseVoid> updateLog(Long id, String keyword) {
        // 参数校验
        BaseExceptionUtil.throwException(NumberUtil.numberCheck(id) && StringUtils.isNotBlank(keyword), "缺少参数");

        // 执行更新
        this.searchLogDao.updateLog(id, keyword);

        return BaseResponseUtil.success();
    }
}
